<template>
    <div class="customer-list">
        <el-input placeholder="搜索客户" v-model="searchQuery" class="search-input" prefix-icon="el-icon-search"></el-input>

        <el-scrollbar class="scroll-container">
            <el-menu 
                :default-active="activeCustomer" 
                class="customer-menu" 
                @select="handleSelect"
                active-text-color="#409EFF"
            >
                <el-menu-item 
                    v-for="customer in filteredCustomers" 
                    :key="customer.id" 
                    :index="customer.id.toString()"
                    class="customer-menu-item"
                >
                    <div class="customer-item">
                        <el-avatar :src="customer.avatar" size="small"></el-avatar>
                        <div class="customer-info">
                            <span class="customer-name">{{ customer.name }}</span>
                            <span class="last-message">{{ customer.lastMessage }}</span>
                        </div>
                        <el-badge v-if="customer.unread > 0" :value="customer.unread" class="unread-badge"></el-badge>
                    </div>
                </el-menu-item>
            </el-menu>
        </el-scrollbar>
    </div>
</template>

<script>
export default {
  name: 'CustomerList',
  data() {
    return {
      searchQuery: '',
      activeCustomer: '1',
      customers: [
        {
          id: 1,
          name: '张三',
          avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
          lastMessage: '您好，我想咨询产品问题',
          unread: 2,
          status: 'online'
        },
        {
          id: 2,
          name: '李四',
          avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png',
          lastMessage: '订单什么时候发货？',
          unread: 0,
          status: 'offline'
        },
        {
          id: 3,
          name: '王五',
          avatar: 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png',
          lastMessage: '谢谢你们的帮助！',
          unread: 1,
          status: 'online'
        }
      ]
    }
  },
  computed: {
    filteredCustomers() {
      return this.customers.filter(customer => 
        customer.name.includes(this.searchQuery)
    )}
  },
  methods: {
    handleSelect(index) {
      this.activeCustomer = index
      const selectedCustomer = this.customers.find(c => c.id.toString() === index)
      this.$emit('customer-selected', selectedCustomer)
    }
  }
}
</script>

<style scoped>
.customer-list {
    height: 100%;
    display: flex;
    flex-direction: column;
    background-color: #fff;
}

.search-input {
    padding: 15px;
    background-color: #f5f7fa;
}

.search-input >>> .el-input__inner {
    background-color: #fff;
}

.scroll-container {
    flex: 1;
    overflow: hidden;
}

.customer-menu {
    border-right: none;
}

.customer-menu-item {
    height: 70px;
    padding: 0 15px;
    margin: 5px 0;
    transition: all 0.3s;
}

.customer-menu-item:hover {
    background-color: #f5f7fa;
}

.customer-menu-item.is-active {
    background-color: #ecf5ff;
}

.customer-item {
    display: flex;
    align-items: center;
    width: 100%;
    position: relative;
    height: 100%;
}

.customer-info {
    display: flex;
    flex-direction: column;
    margin-left: 12px;
    flex: 1;
    overflow: hidden;
}

.customer-name {
    font-size: 14px;
    font-weight: 500;
    margin-bottom: 4px;
    color: #303133;
}

.last-message {
    font-size: 12px;
    color: #909399;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
}

.unread-badge {
    position: absolute;
    right: 10px;
}

/* 调整徽标样式 */
.unread-badge >>> .el-badge__content {
    height: 18px;
    line-height: 18px;
    padding: 0 5px;
    font-size: 10px;
}
</style>